package com.itheima.controller;

import com.google.common.base.Strings;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.resources.DateUtils;
import com.itheima.resources.MessageConstant;
import com.itheima.resources.POIUtils;
import com.itheima.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RequestMapping(value = "/orderSetting",name = "预约设置模块")
@RestController
public class OrderSettingController {
    //注入生产者对象
    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping(value = "/importOrderSettings.do",name="文件上传")
    public Result importOrderSettings(@RequestParam("excelFile")MultipartFile file) throws IOException {

        try {
            //定义一个封装OrderSetting对象的list集合
            List<OrderSetting> orderSettingList=new ArrayList<>();

            //使用工具类 将上传的file文件对象数据封装进list<String>集合中
            List<String[]> readExcel = POIUtils.readExcel(file);


            //数据转换
            for (String[] strings : readExcel) {
                //创建一个OrderSetting对象
                OrderSetting orderSetting = new OrderSetting();

                //javaBean中orderDate属性为date类型,所以使用工具类将Strings[0] 的数据封装给orderDate
                Date date = DateUtils.parseString2Date(strings[0],"yyyy/MM/dd");
                orderSetting.setOrderDate(date);

                //赋值 预约数
                orderSetting.setNumber(Integer.parseInt(strings[1]));

                //将对象添加到集合
                orderSettingList.add(orderSetting);
            }
            //调用生产者
            orderSettingService.importOrderSettings(orderSettingList);

            //执行成功返回对象
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping(value = "/findOrderSetting.do",name = "查询当前月的所有预约设置数据")
    public Result findOrderSetting(Integer currentYear,Integer currentMonth){
        try {
            //调用生产者查询数据  返回值为List<Map>
            List<Map> list=orderSettingService.findOrderSetting(currentYear,currentMonth);

            //查询成功
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(true,MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    @RequestMapping(value = "/updateOrderSetting.do",name = "修改当天的预约设置")
    public Result updateOrderSetting(Integer year,Integer month,Integer day,Integer number){

        //转为数字加一

        try {
            //调用生产者修改数据  month+1 因为前端传递的数字会小 一个月
            orderSettingService.updateOrderSetting(year,month+1,day,number);

            //修改成功
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_FAIL);
        }

    }
}
